﻿using System;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff
{
    struct RangeAddress8
    {
        private ushort firstRow;
        private ushort lastRow;
        private byte firstColumn;
        private byte lastColumn;
#if DEBUG
        public override string ToString()
        {
            return string.Format("{{firstRow {0} firstColumn {1} lastRow {2} lastColumn {3}}}", firstRow, firstColumn, lastRow, lastColumn);
        }
#endif
        public void Read(BinaryReader reader)
        {
            this.firstRow = reader.ReadUInt16();
            this.lastRow = reader.ReadUInt16();
            this.firstColumn = reader.ReadByte();
            this.lastColumn = reader.ReadByte();

        }
        public static explicit operator RangeAddress(RangeAddress8 ra)
        {
            return new RangeAddress(new CellAddress(ra.firstRow, ra.firstColumn), new CellAddress(ra.lastRow, ra.lastColumn));

        }
        public static explicit operator RangeAddress8(RangeAddress ra)
        {
            return new RangeAddress8
                       {
                           firstRow = (ushort) ra.TopLeft.Row,
                           firstColumn =
                               (byte) ra.TopLeft.Column,
                           lastRow = (ushort) ra.BottomRight.Row,
                           lastColumn = (byte) ra.BottomRight.Column
                       };

        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(firstRow);
            writer.Write(lastRow);
            writer.Write(firstColumn);
            writer.Write(lastColumn);
        }
    }
}